package com.example.demo.web.rest.controller;

import com.ffcs.oss.annotation.PermissionAuth;
import com.example.demo.client.SmQryClient;
import com.example.demo.client.evt.sysbasequery.QuerySystemConfigEvt;
import com.example.demo.client.vm.sysbasequery.SystemConfigVm;
import com.example.demo.domain.SystemConfig;
import com.example.demo.enums.SysConfigEnum;
import com.example.demo.enums.YesOrNo;
import com.ffcs.oss.param.out.ServiceResp;
import com.ffcs.oss.param.vm.QueryPageVm;
import com.example.demo.service.SystemConfigService;
import com.example.demo.service.UserService;
import com.example.demo.web.rest.evt.IdEvt;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 系统常量配置管理
 * @Author zhengmf
 * @Date 2018/12/27  17:57
 */
@Api("URL配置")
@RestController
@RequestMapping("/api/systemConfig")
public class SystemConfigController extends BaseController{
    @Autowired
    private SystemConfigService systemConfigService;
    @Autowired
    private SmQryClient smQryClient;
    @Autowired
    private UserService userService;

    @ApiOperation("刷新系统常量缓存")
    @PostMapping(value = "/refreshAll")
    public ServiceResp refreshAllSystemConfig(){
        return baseQueryClient.refreshAllSystemConfig();
    }

    @ApiOperation("查询系统常量")
    @PostMapping(value = "/querySystemConfig")
    public ServiceResp<QueryPageVm<SystemConfigVm>> querySystemConfig(@RequestBody QuerySystemConfigEvt evt){
        return smQryClient.querySystemConfig(evt);
    }

    @ApiOperation("保存系统常量")
    @PostMapping(value = "/saveSystemConfig")
    public ServiceResp<Boolean> saveSystemConfig(@RequestBody @Validated SystemConfig systemConfig){
        systemConfigService.saveSysConfig(systemConfig);
        smQryClient.clearSysConfigCache();
        return ServiceResp.getInstance(Boolean.class).success(true);
    }

    @ApiOperation("保存安全策略配置")
    @PermissionAuth(code = "OSS-SETTING-SECURITY-POLICY")
    @PostMapping(value = "/saveSecurityPolicy")
    public ServiceResp<Boolean> saveSecurityPolicy(@RequestBody @Validated List<SystemConfig> systemConfigList){
        for (SystemConfig systemConfig : systemConfigList) {
            //如果是勾选修改密码的字段，批量修改用户配置表

            if(systemConfig.getKey().equals(SysConfigEnum.CHANGE_PWD_NEXTTIME_LOGIN.getKey()) && systemConfig.getValue().equals(YesOrNo.Y.getValue()))
            {
                userService.updateMustUpdStatus();
            }
            systemConfigService.saveSysConfig(systemConfig);
        }
        smQryClient.clearQryCache();
        return ServiceResp.getInstance(Boolean.class).success(true);
    }

    @ApiOperation("根据id删除系统常量，逻辑删除")
    @PostMapping(value = "/delSystemConfigById")
    public ServiceResp<Boolean> delSystemConfigById(@RequestBody IdEvt evt){
        systemConfigService.updateStatus(evt.getId());
        baseQueryClient.refreshAllSystemConfig();
        return ServiceResp.getInstance(Boolean.class).success(true);
    }

}
